<template>
  <div class="pagination-wrapper">
    <el-pagination
      :background="background"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      :total="total"
      :layout="layout"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { computed, defineProps, defineEmits } from 'vue'

// 定义组件名称
defineOptions({
  name: 'Pagination'
})

// 定义props
const props = defineProps({
  page: {
    type: Number,
    default: 1,
    required: true
  },
  limit: {
    type: Number,
    default: 10,
    required: true
  },
  background: {
    type: Boolean,
    default: true
  },
  pageSizes: {
    type: Array,
    default: () => [10, 15, 20, 50]
  },
  total: {
    type: Number,
    default: 0,
    required: true
  },
  layout: {
    type: String,
    default: 'total, prev, pager, next, sizes, jumper'
  }
})

// 定义emits
const emit = defineEmits(['update:page', 'update:limit', 'pagination'])

// 计算属性
const currentPage = computed({
  get() {
    return props.page
  },
  set(val) {
    emit('update:page', val)
  }
})

const pageSize = computed({
  get() {
    return props.limit
  },
  set(val) {
    emit('update:limit', val)
  }
})

// 方法
const handleSizeChange = (val) => {
  emit('pagination', { page: currentPage.value, limit: val })
}

const handleCurrentChange = (val) => {
  emit('pagination', { page: val, limit: pageSize.value })
}
</script>

<style lang="less">
.pagination-wrapper{
  margin-top: 25px;
  .el-pagination{
    float: right;
  }
}
</style>
